package com.fdb.efp.flow.api.rest;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.fdb.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.flow.service.facade.BizEventInfoService;
import com.fdb.efp.flow.service.vo.BizEventInfoVO;
import com.fdb.basic.framework.core.constant.ApplicationConstance;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class BizEventInfoController extends AbstractController  {
	
	@Autowired
	@Qualifier("bizEventInfoService")
	private BizEventInfoService bizEventInfoService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/biz/event/infos", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizEventInfoVO>> queryBizEventInfoAll(
			@RequestBody BizEventInfoVO vo) {		
		// List<BizEventInfoVO> outputVo = this.executeQueryList("queryAll",
		// bizEventInfoService, new Object[]{vo});
		// 根据用户角色获取用户权限方案
		List<BizEventInfoVO> outputVo = null;
		//Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		//DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType("01");
		//setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = bizEventInfoService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = bizEventInfoService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = bizEventInfoService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = bizEventInfoService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/biz/event/info/{bizEventId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<BizEventInfoVO> queryByPk(@PathVariable("bizEventId") String bizEventId) {		
		BizEventInfoVO inVo = new BizEventInfoVO();
				inVo.setBizEventId(bizEventId);
		BizEventInfoVO outputVo = bizEventInfoService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param bizEventInfo
	 * @return
	 */
	@RequestMapping(value="/biz/event/info", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody BizEventInfoVO inBizEventInfoVo) {		
		int outputVo = bizEventInfoService.deleteByPk(inBizEventInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inBizEventInfoVo
	 * @return
	 */
	@RequestMapping(value="/biz/event/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody BizEventInfoVO inBizEventInfoVo) {		
		int outputVo = bizEventInfoService.updateByPk(inBizEventInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inBizEventInfoVo
	 * @return
	 */
	@RequestMapping(value="/biz/event/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertBizEventInfo(@RequestBody BizEventInfoVO inBizEventInfoVo) {
		BizEventInfoVO vo = bizEventInfoService.queryByPk(inBizEventInfoVo);
		if(Objects.nonNull(vo)) {
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setMessage("保存失败，该事件标识已存在！");
			responseData.setCode(ApplicationConstance.FAILURE);
			responseData.setSuccess(false);
			return responseData;
		}
		int outputVo = bizEventInfoService.insertBizEventInfo(inBizEventInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/biz/event/infosByFlowType", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizEventInfoVO>> queryAllByFlowType(
			@RequestBody BizEventInfoVO vo) {		
		List<BizEventInfoVO> outputVo = bizEventInfoService.queryAllByFlowType(vo);
		return getResponseData(outputVo);
	}
	
}
